﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;
using BLL;
using System.Text.RegularExpressions;

namespace net第六次作业
{
    public partial class Form1 : Form
    {
        private string itemType = string.Empty;
        private SchoolService schoolService = new SchoolService();
        private ClassService classService = new ClassService();
        private StudentService studentService = new StudentService();
        private LogService logService = new LogService();
        private int modifyId = 0;

        public Form1()
        {
            InitializeComponent();
        }


        //各种实体的创建类型不同
        //可以用text不可输入，但是感觉不是很好看
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(comboBox1.SelectedIndex == 0)
            {
                itemType = "school";
                nameLabel.Text = "学校名称";
                nameLabel.Visible = true;
                nameText.Visible = true;
                idLabel.Visible = false;
                idText.Visible = false;
                ageLabel.Visible = false;
                ageText.Visible = false;
            }
            else if(comboBox1.SelectedIndex == 1)
            {
                itemType = "class";
                nameLabel.Text = "班级名称";
                idLabel.Text = "学校ID";           
                nameLabel.Visible = true;
                nameText.Visible = true;
                idLabel.Visible = true;
                idText.Visible = true;
                ageLabel.Visible = false;
                ageText.Visible = false;
            }
            else if(comboBox1.SelectedIndex == 2)
            {
                itemType = "student";
                nameLabel.Text = "学生姓名";
                idLabel.Text = "班级ID";
                nameLabel.Visible = true;
                nameText.Visible = true;
                idLabel.Visible = true;
                idText.Visible = true;
                ageLabel.Visible = true;
                ageText.Visible = true;
            }
        }

        //添加
        private void add_Click(object sender, EventArgs e)
        {
            try
            {
                if (itemType == "school")
                {
                    if (nameText.Text != "")
                    {
                        School school = new School();
                        school.SchoolName = nameText.Text;
                        schoolService.AddSchool(school);
                    }
                    else
                    {
                        MessageBox.Show("名字不能为空!");
                    }
                }
                else if (itemType == "class")
                {
                    if (nameText.Text != "" && idText.Text != "")
                    {
                        Class c = new Class();
                        c.ClassName = nameText.Text;
                        c.SchoolID = Convert.ToInt32(idText.Text);
                        classService.AddClass(c);
                    }
                    else
                    {
                        MessageBox.Show("请填入完整信息!");
                    }
                }
                else if (itemType == "student")
                {
                    if (nameText.Text != "" && idText.Text != "" && ageText.Text != "")
                    {
                        Student student = new Student();
                        student.StudentName = nameText.Text;
                        student.ClassID = Convert.ToInt32(idText.Text);
                        student.Age = Convert.ToInt32(ageText.Text);
                        studentService.AddStudent(student);
                    }
                    else
                    {
                        MessageBox.Show("请填入完整信息");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择一项进行操作！");
                }

                logService.AddLog("添加", itemType,0," ");
                MessageBox.Show("添加成功");
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                nameText.Clear();
                idText.Clear();
                ageText.Clear();
            }
        }

        //这里同时实现了对三种实体的查询，耗时会比较大
        //更好的方法是分开查询，但是工作量已经很大了，没有分开实现
        private void search_Click(object sender, EventArgs e)
        {
            try {
                searchResult.DataSource = null;
                List<School> s = schoolService.GetSchools(searchText.Text);
                List<Class> c = classService.GetClasses(searchText.Text);
                List<Student> stu = studentService.GetStudents(searchText.Text);
                List<String> list = new List<String>();
                foreach (School school in s)
                {
                    list.Add(school.ToString());
                }
                foreach(Class cl in c)
                {
                    list.Add(cl.ToString());
                }
                foreach(Student student in stu)
                {
                    list.Add(student.ToString()); 
                }
                searchResult.DataSource = list;
                logService.AddLog("搜索", searchText.Text, 0, " ");
            }
            catch(Exception ex )
            {
                MessageBox.Show(ex.Message);
            }
        }

        //提取ID
        public int ExtractID(string input)
        {
            var match = Regex.Match(input, @"ID:\s*(\d+)");
            if (match.Success)
            {
                Console.WriteLine(input);
                return int.Parse(match.Groups[1].Value);
            }
            else
            {
                throw new FormatException("ID not found in the input string.");
            }
        }

        //删除
        //用了级联删除
        //在这里进行二次确认
        private void delete_Click(object sender, EventArgs e)
        {
            try
            {
                if(searchResult.SelectedItems != null)
                {
                    string selectedText = searchResult.SelectedItem.ToString();
                    if (selectedText.StartsWith("Student:"))
                    {
                        int studentId = ExtractID(selectedText);
                        studentService.DeleteStudent(studentId);
                        logService.AddLog("删除", "Student", 0, " ");
                    }
                    else if (selectedText.StartsWith("Class:"))
                    {
                        DialogResult result = MessageBox.Show("这将删除整个班学生的所有信息！","确认删除", MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
                        if(result == DialogResult.Yes)
                        {
                            int classId = ExtractID(selectedText);
                            classService.DeleteClass(classId);
                            logService.AddLog("删除", "Class", 0, " ");
                        }
                    }
                    else if(selectedText.StartsWith("School:"))
                    {
                        DialogResult result = MessageBox.Show("这将删除全部班级以及全部学生的信息！", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (result == DialogResult.Yes)
                        {
                            int schoolId = ExtractID(selectedText);
                            schoolService.DeleteSchool(schoolId);
                            logService.AddLog("删除", "School", 0, " ");
                        }
                        
                    }
                    
                    MessageBox.Show("删除成功！");
                }
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //先点击修改进入修改模式
        private void modify_Click(object sender, EventArgs e)
        {
            if(searchResult.SelectedItems != null)
            {
                add.Visible = false;
                confirm.Visible = true;
                string selectedText = searchResult.SelectedItem.ToString();
                //获取修改的ID
                //在下面确定修改的类型
                modifyId = ExtractID(selectedText);
                if (selectedText.StartsWith("Student:"))
                {
                    comboBox1.SelectedIndex = 2;

                }
                else if (selectedText.StartsWith("Class:"))
                {
                    comboBox1.SelectedIndex = 1;
                }
                else if (selectedText.StartsWith("School:"))
                {
                    comboBox1.SelectedIndex = 0;
                }
            }
        }

        //通过ID定位修改
        //修改完之后重新搜索就会不见
        private void confirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (itemType == "school")
                {
                    if (nameText.Text != "")
                    {
                        School school = new School();
                        school.SchoolName = nameText.Text;
                        school.SchoolID = modifyId;
                        schoolService.UpdateSchool(school);
                    }
                    else
                    {
                        MessageBox.Show("名字不能为空!");
                    }
                }
                else if (itemType == "class")
                {
                    if (nameText.Text != "" && idText.Text != "")
                    {
                        Class c = new Class();
                        c.ClassName = nameText.Text;
                        c.SchoolID = Convert.ToInt32(idText.Text);
                        c.ClassID = modifyId;
                        classService.UpdateClass(c);
                    }
                    else
                    {
                        MessageBox.Show("请填入完整信息!");
                    }
                }
                else if (itemType == "student")
                {
                    if (nameText.Text != "" && idText.Text != "" && ageText.Text != "")
                    {
                        Student student = new Student();
                        student.StudentName = nameText.Text;
                        student.ClassID = Convert.ToInt32(idText.Text);
                        student.Age = Convert.ToInt32(ageText.Text);
                        student.StudentID = modifyId;
                        studentService.UpdateStudent(student);
                    }
                    else
                    {
                        MessageBox.Show("请填入完整信息");
                    }
                }
                else
                {
                    MessageBox.Show("请先选择一项进行操作！");
                }
                MessageBox.Show("修改成功");
                logService.AddLog("修改", itemType, 0, " ");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                nameText.Clear();
                idText.Clear();
                ageText.Clear();
            }
        }

        //这里加了个按任意键退出
        private void history_Click(object sender, EventArgs e)
        {
            try
            {
                showHistory.Visible = true;
                showHistory.DataSource = null;
                showHistory.DataSource = logService.GetAllLogs("");
                showHistory.DisplayMember = "displayInfo";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        //下面是历史记录的查看及搜索
        //由于代码量已经很大了，这里只用了四种类型的操作记录分类搜索和全部记录查看
        private void showHistory_KeyPress(object sender, KeyPressEventArgs e)
        {
            showHistory.Visible = false;
            showHistory.DataSource = null;
        }

        private void clearLog_Click(object sender, EventArgs e)
        {
            logService.ClearLogs();
            MessageBox.Show("清楚成功！");
        }

        private void addHistory_Click(object sender, EventArgs e)
        {
            try
            {
                showHistory.Visible = true;
                showHistory.DataSource = null;
                showHistory.DataSource = logService.GetAllLogs("添加");
                showHistory.DisplayMember = "displayInfo";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void modifyHistory_Click(object sender, EventArgs e)
        {
            try
            {
                showHistory.Visible = true;
                showHistory.DataSource = null;
                showHistory.DataSource = logService.GetAllLogs("修改");
                showHistory.DisplayMember = "displayInfo";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void deleteHistory_Click(object sender, EventArgs e)
        {
            try
            {
                showHistory.Visible = true;
                showHistory.DataSource = null;
                showHistory.DataSource = logService.GetAllLogs("删除");
                showHistory.DisplayMember = "displayInfo";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void searchHistory_Click(object sender, EventArgs e)
        {
            try
            {
                showHistory.Visible = true;
                showHistory.DataSource = null;
                showHistory.DataSource = logService.GetAllLogs("搜索");
                showHistory.DisplayMember = "displayInfo";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
